Question 1: Use yfinance to Extract Stock Data

Reset the index, save, and display the first five rows of the tesla_data dataframe using the head function. Upload a screenshot of the results and code from the beginning of Question 1 to the results below.

In [1]:
import requests
import pandas as pd
import yfinance as yf
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
In [2]:
def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data.Date, infer_datetime_format=True), y=stock_data.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data.Date, infer_datetime_format=True), y=revenue_data.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()
In [3]:
tesla=yf.Ticker("TSLA")
tesla_data=tesla.history(period='max')
tesla_data.reset_index(inplace=True)
tesla_data.tail()
Out[3]:
Date Open High Low Close Volume Dividends Stock Splits
2809 2021-08-25 707.030029 716.969971 704.000000 711.200012 12645600 0 0.0
2810 2021-08-26 708.309998 715.400024 697.619995 701.159973 13214300 0 0.0
2811 2021-08-27 705.000000 715.000000 702.099976 711.919983 13762100 0 0.0
2812 2021-08-30 714.719971 731.000000 712.729980 730.909973 18604200 0 0.0
2813 2021-08-31 733.000000 740.390015 726.440002 735.719971 20643200 0 0.0

Question 2: Use Webscraping to Extract Tesla Revenue Data

Display the last five rows of the tesla_revenue dataframe using the tail function. Upload a screenshot of the results.

In [4]:
url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
html_data = requests.get(url).text
In [5]:
soup = BeautifulSoup(html_data, "html.parser")
soup.find_all('title')
Out[5]:
[<title>Tesla Revenue 2009-2021 | TSLA | MacroTrends</title>]
In [6]:
tesla_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])

for row in soup.find_all("tbody")[1].find_all("tr"):
    col = row.find_all("td")
    date = col[0].text
    revenue = col[1].text.replace("$", "").replace(",", "")
    
    tesla_revenue = tesla_revenue.append({"Date": date, "Revenue": revenue}, ignore_index = True)
In [7]:
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
In [8]:
tesla_revenue.tail()
Out[8]:
Date Revenue
43 2010-09-30 31
44 2010-06-30 28
45 2010-03-31 21
47 2009-09-30 46
48 2009-06-30 27

Question 3: Use yfinance to Extract Stock Data

Reset the index, save, and display the first five rows of the gme_data dataframe using the head function. Upload a screenshot of the results and code from the beginning of Question 1 to the results below.

In [9]:
gme=yf.Ticker("GME")
gme_data=gme.history(period='max')
gme_data.reset_index(inplace=True)
gme_data.tail()
Out[9]:
Date Open High Low Close Volume Dividends Stock Splits
4917 2021-08-25 206.649994 227.000000 193.000000 199.649994 12861100 0.0 0.0
4918 2021-08-26 200.679993 217.000000 199.000000 205.220001 6238600 0.0 0.0
4919 2021-08-27 207.699997 213.000000 200.020004 204.949997 3224900 0.0 0.0
4920 2021-08-30 205.000000 218.190002 203.020004 209.199997 3309600 0.0 0.0
4921 2021-08-31 212.699997 222.300003 211.460007 218.240005 3566800 0.0 0.0

Question 4: Use Webscraping to Extract GME Revenue Data

Display the last five rows of the gme_revenue dataframe using the tail function. Upload a screenshot of the results.

In [10]:
url = "https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue"
html_data = requests.get(url).text
In [11]:
soup = BeautifulSoup(html_data, "html.parser")
soup.find_all('title')
Out[11]:
[<title>GameStop Revenue 2006-2021 | GME | MacroTrends</title>]
In [12]:
gme_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])

for row in soup.find_all("tbody")[1].find_all("tr"):
    col = row.find_all("td")
    date = col[0].text
    revenue = col[1].text.replace("$", "").replace(",", "")
    
    gme_revenue = gme_revenue.append({"Date": date, "Revenue": revenue}, ignore_index = True)
In [13]:
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
gme_revenue.tail()
Out[13]:
Date Revenue
61 2006-01-31 1667
62 2005-10-31 534
63 2005-07-31 416
64 2005-04-30 475
65 2005-01-31 709

Question 5: Plot Tesla Stock Graph

Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph.

In [14]:
make_graph(tesla_data, tesla_revenue, 'Tesla')

Question 6: Plot GameStop Stock Graph

Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph.

In [16]:
make_graph(gme_data, gme_revenue, 'GameStop')
In [ ]: